package com.itheima.mapper.system;

import com.itheima.domain.system.Permission;
import com.itheima.domain.system.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface RoleMapper {


    @Select("select * from role")
    List<Role> findAll();



    @Insert("insert into role values(#{id},#{roleName},#{roleDesc})")
    void save(Role role);

    @Delete("delete from role where id = #{id}")
    void delete(String id);

    @Select("select p.id from permission as p where p.id " +
            "in(select permissionId from role_permission where roleId=#{id})")
    List<String> findPermById(String id);



    @Insert("insert into role_permission values(#{pid},#{rid})")
    void saveRolePerm(@Param("rid")String rid, @Param("pid")String pid);

    @Delete("delete from role_permission where roleId=#{id}")
    void deletePermRole(String id);

    @Delete("delete from users_role where roleId=#{id}")
    void deleteUserRole(String id);

    @Delete("delete from role where id=#{id}")
    void deleteById(String id);

    @Select("select * from role where id=#{rid}")
    @ResultMap("roleMap")
    Role findRoleByRid(String rid);


    @Select("select * from role where id in(select roleId from users_role where userId=#{uid})")
    @Results(id = "roleMap",value = {
            @Result(id = true,column = "id",property = "id"),
            @Result(column = "roleName",property = "roleName"),
            @Result(column = "roleDesc",property = "roleDesc"),
            @Result(javaType = List.class,column = "id",property = "permissions",
                    many = @Many(select = "com.itheima.mapper.system.PermissionMapper.findPermsByRid")),
            @Result(javaType = List.class,column = "id",property = "users",
                    many = @Many(select = "com.itheima.mapper.system.UserMapper.findUserByRid"))
    })
    List<Role> findRolesByUid(String uid);


    @Select("select * from role where roleName like #{roleName}")
    List<Role> selectLike(String roleName);
}
